package com.wabbei.friendcenter.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.webbei.friendcenter.service.MyFriendCenter;
import com.webbei.pojo.Messages;

public class SendMessageEachOtherAction extends ActionSupport  {
	private static final long serialVersionUID = 1L;
	private MyFriendCenter friservice;
	private String messageContent;
	private String fromName;
	private String toName;
	private Date date;
	private Messages message;
	private String uname;
	private File[] imageandfile;
	private String[] imageandfileFileName;
	
	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public File[] getImageandfile() {
		return imageandfile;
	}

	public void setImageandfile(File[] imageandfile) {
		this.imageandfile = imageandfile;
	}

	public String[] getImageandfileFileName() {
		return imageandfileFileName;
	}

	public void setImageandfileFileName(String[] imageandfileFileName) {
		this.imageandfileFileName = imageandfileFileName;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getFromName() {
		return fromName;
	}

	public void setFromName(String fromName) {
		this.fromName = fromName;
	}

	public String getToName() {
		return toName;
	}

	public void setToName(String toName) {
		this.toName = toName;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	
	public String getMessageContent() {
		return messageContent;
	}

	public void setMessageContent(String messageContent) {
		this.messageContent = messageContent;
	}

	public Messages getMessage() {
		return message;
	}

	public void setMessage(Messages message) {
		this.message = message;
	}

	public MyFriendCenter getFriservice() {
		return friservice;
	}

	public void setFriservice(MyFriendCenter friservice) {
		this.friservice = friservice;
	}
	
	public String execute(){
		//fromName=ActionContext.getContext().getSession().get("");
		fromName=(String)ServletActionContext.getContext().getSession().get("username");
		date=new Date();
		message=new Messages();
		message.setFromName(fromName);
		message.setToName(toName);
		message.setMessageTime(date);
		message.setMessageContent(messageContent);
		if(imageandfile==null){
			System.out.println("file null  1");
			message.setMessagePic("");
			message.setCDRfilePath("");
			System.out.println("send message:"+message);
			friservice.insertMessageEachOther(message);
		}else{
			if(imageandfile.length==1){
				String file_ext=imageandfileFileName[0].substring(imageandfileFileName[0].indexOf('.'),imageandfileFileName[0].length());
				imageandfileFileName[0]=ReNameUtil.GetNewName()+file_ext;
				message.setMessagePic(imageandfileFileName[0]);
				friservice.insertMessageEachOther(message);
			}else if(imageandfile.length==2){
				for(int i=0;i<2;i++){
					if(i==0){
						String file_ext=imageandfileFileName[0].substring(imageandfileFileName[0].indexOf('.'),imageandfileFileName[0].length());
						imageandfileFileName[0]=ReNameUtil.GetNewName()+file_ext;
						message.setMessagePic(imageandfileFileName[0]);
					}else if(i==1){
						String file_ext=imageandfileFileName[1].substring(imageandfileFileName[1].indexOf('.'),imageandfileFileName[1].length());
						imageandfileFileName[1]=ReNameUtil.GetNewName()+file_ext;
						message.setCDRfilePath(imageandfileFileName[1]);
					}
				}
				friservice.insertMessageEachOther(message);
		}
		if(imageandfile!=null){
			System.out.println("imageandfilelength:"+imageandfile.length);
			for(String s:imageandfileFileName){
				System.out.println(s);
			}
				String realPath=ServletActionContext.getServletContext().getRealPath("/"+toName);
				File newFile=new File(realPath);
				if(!newFile.exists()){
					newFile.mkdirs();
				}
				for(int i=0;i<imageandfile.length;i++){
						try {
							FileUtils.copyFile(imageandfile[i],new File(newFile,imageandfileFileName[i]));
						} catch (IOException e) {
							e.printStackTrace();
						}
				}
				System.out.println("send file successful");
		}
		}
		//send success then clean imageandfile and imageandfileFileName
		imageandfile=null;
		imageandfileFileName=null;
		try {
			ServletActionContext.getResponse().getWriter().println("<script>parent.callbackMessage('success')</script>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
